﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BankRemoteSystem.GUI
{
    public partial class frmInfo : Form
    {
        DAL.Connection con;
        Entities.User us = new BankRemoteSystem.Entities.User();
        BL.AccountBO a = new BankRemoteSystem.BL.AccountBO();
        Entities.Account acc;
        DataSet dtaBank; 
        int ac;
        public frmInfo(Entities.User u,int i)
        {
            InitializeComponent();
            this.us = u;
            this.ac = i;
            acc = a.getAccoutbyUserID(us.UserID);
            con = new DAL.Connection();
            dtaBank = new DataSet();
            BL.AccountBO abo = new BL.AccountBO();
            dtaBank = abo.getAllAccount();
        }

        private void frmInfo_Load(object sender, EventArgs e)
        {
            if (ac == 0)
            {
                btnSave.Enabled = false;
                btnWithdraw.Enabled = false;
                this.Text = "Checking";
            }
            else if (ac == 1)
            {
                btnWithdraw.Enabled = false;
                this.Text = "Savings";
            }
            else if (ac == 2)
            {
                this.Text = "Credit Card";
            }

            Display();
            
        }
        public void Display()
        {
            lblGetAcc.Text = us.FullName;                        
            lblGetbalance.Text = acc.Balance.ToString();

            if (ac == 0) lblTranfertype.Text = "Checking";
            else if (ac == 1) lblTranfertype.Text = "Saving";
            else lblTranfertype.Text = "Credit Card";

            pnlSave.Visible = false;
            pnlTransfer.Visible = false;
            pnlWithDraw.Visible = false;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            pnlTransfer.Visible = false;
            pnlWithDraw.Visible = false;
            pnlSave.Visible = true;
            pnlSave.Location = new Point(320, 285);                
        }

        private void btnTransfer_Click(object sender, EventArgs e)
        {
            pnlSave.Visible = false;
            pnlWithDraw.Visible = false;
            pnlTransfer.Visible = true;
            pnlTransfer.Location =new Point(320, 285);
            AddItemsInComboBox();

        }

        private void AddItemsInComboBox()
        {
            cbAccountID.Items.Clear();
            for (int comboBoxIndex = 0; comboBoxIndex < dtaBank.Tables["Account"].Rows.Count; comboBoxIndex++)
            {
                cbAccountID.Items.Add(dtaBank.Tables["Account"].Rows[comboBoxIndex].ItemArray[0].ToString());
            }
        }

        private void btnWithdraw_Click(object sender, EventArgs e)
        {
            pnlSave.Visible = false;
            pnlTransfer.Visible = false;
            pnlWithDraw.Visible = true;
            pnlWithDraw.Location = new Point(320, 285);
        }

        

        private void btnCancel_Click(object sender, EventArgs e)
        {
            frmInfo_Load(sender, e);
        }

        private void btnSubmitSave_Click(object sender, EventArgs e)
        {
            if (txtSaving.Text.Equals(""))
            {
                MessageBox.Show("enter money");
            }
            else
            {
                int saving = Convert.ToInt32(txtSaving.Text);
                if (saving < 0)
                {
                    MessageBox.Show("not true!!");
                }
                else
                {
                    BL.AccountBO abo = new BL.AccountBO();
                    if (abo.updateBalance(acc.AccountID, acc.Balance + saving))
                    {
                        MessageBox.Show("Money's saved!");
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("error");
                    }
                }
            }
        }

        private void btnSubmitWithdraw_Click(object sender, EventArgs e)
        {
            if (txtWithdraw.Text.Equals(""))
            {
                MessageBox.Show("enter money");
            }
            else
            {
                int withdraw = Convert.ToInt32(txtWithdraw.Text);
                if (withdraw > acc.Balance)
                {
                    MessageBox.Show("Over!!");
                }
                else
                {
                    BL.AccountBO abo = new BL.AccountBO();
                    if (abo.updateBalance(acc.AccountID, acc.Balance - withdraw))
                    {
                        MessageBox.Show("ok");
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("error");
                    }
                }


            }
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            Entities.Transaction trans = new Entities.Transaction();
            BL.TransactionBO transBO = new BankRemoteSystem.BL.TransactionBO();
            if (txtTransfer.Text.Equals(""))
            {
                MessageBox.Show("enter money");
                txtTransfer.Focus();
            }
            if (cbAccountID.Text.Equals(""))
            {
                MessageBox.Show("enter Account");
                cbAccountID.Focus();
            }
            else
            {
                int transfer = Convert.ToInt32(txtTransfer.Text);
                if (transfer > acc.Balance)
                {
                    MessageBox.Show("Over!!");
                }
                else
                {
                    BL.AccountBO abo = new BL.AccountBO();
                    trans.AccountID = acc.AccountID;
                    trans.Action = "Transfer to " + acc.AccountID;
                    trans.Date = DateTime.Now;
                    if ((abo.updateBalance(acc.AccountID, acc.Balance - transfer)) && (abo.addBalance(cbAccountID.Text, transfer)) && (transBO.insertTrans(trans)))
                    {
                        MessageBox.Show("ok");
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("error");
                    }
                }
            }
        }
    }
}
